package cn.portx.view;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import cn.portx.entity.Customer;
import cn.portx.entity.ExpenseItem;
import cn.portx.entity.Memorandum;
import cn.portx.entity.Order;
import cn.portx.entity.OrderExpense;

import com.bstek.bdf3.dorado.jpa.JpaUtil;
import com.bstek.bdf3.dorado.jpa.policy.SaveContext;
import com.bstek.bdf3.dorado.jpa.policy.impl.SmartSavePolicyAdapter;
import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Page;

@Component
public class YsfypllrPR {

	private int ystype = 0;
	private int yftype = 1;

	@Autowired
	private ExportPR entrancePR;

	@Transactional(readOnly = true)
	@DataProvider
	public void loadOrder(Page<Map<String, Object>> page, Criteria criteria) {
		JpaUtil.linq(Order.class).where(criteria).isNull("targetId").desc("orderNo").paging(page);
	}

	@Transactional
	@DataProvider
	public List<OrderExpense> loadOrderExpense(String orderId) {
		return entrancePR.loadExpense(orderId, ystype);
	}

	@Transactional
	@DataProvider
	public List<OrderExpense> loadOrderExpenseYf(String orderId) {
		return entrancePR.loadExpense(orderId, yftype);
	}

	@Transactional(readOnly = true)
	@DataProvider
	public String loadExpenseItem(Criteria criteria, String expenseItemId) {
		List<ExpenseItem> list = JpaUtil.linq(ExpenseItem.class).where(criteria).equal("id", expenseItemId).list();
		if (list != null) {
			return list.get(0).getName();
		}
		return null;
	}

	@Transactional(readOnly = true)
	@DataProvider
	public List<ExpenseItem> loadExpenseItemIsInCommonUse() {
		return JpaUtil.linq(ExpenseItem.class).equal("inCommonUse", 1).equal("type", "0").list();
	}

	@Transactional(readOnly = true)
	@DataProvider
	public List<ExpenseItem> loadExpenseItemIsInCommonUseYf() {
		return JpaUtil.linq(ExpenseItem.class).equal("inCommonUse", 1).equal("type", "1").list();
	}

	@Transactional
	@DataResolver
	public void saveOrderExpenseByItem(List<Order> orders) {

		JpaUtil.save(orders, new SmartSavePolicyAdapter() {
			@Override
			public boolean beforeUpdate(SaveContext context) {
				Object parent = context.getParent();
				Object entity = context.getEntity();
				if (entity instanceof OrderExpense && parent instanceof Order) {
					OrderExpense expense = (OrderExpense) entity;
					entrancePR.validateOrderExpenseJszt(expense.getId());
				}
				return true;
			}
		});
	}

	@Transactional
	@DataResolver
	public void plSave(List<String> orderId, List<ExpenseItem> items) {
		List<OrderExpense> upList = new ArrayList<OrderExpense>();
		for (String orid : orderId) {
			List<OrderExpense> oeList = JpaUtil.linq(OrderExpense.class).equal("orderId", orid).list();
			for (OrderExpense oe : oeList) {
				this.entrancePR.validateOrderExpenseJszt(oe.getId());
				for (ExpenseItem item : items) {
					if (oe.getExpenseItemId().equals(item.getId())) {
						oe.setExpenseVal(EntityUtils.getDouble(item, "expenseItemVal"));
						upList.add(oe);
					}
				}
			}
		}
		JpaUtil.merge(upList);
	}

	@Transactional(readOnly = true)
	@DataProvider
	public Customer getCustomer(String customerId, Criteria criteria) {
		List<Customer> ls = JpaUtil.linq(Customer.class).equal("id", customerId).where(criteria).list();
		if (ls.size() > 0) {
			return ls.get(0);
		}
		return null;
	}

	@Transactional(readOnly = true)
	@DataProvider
	public List<String> loadMemorandum() {
		List<String> values = JpaUtil.linq(Memorandum.class, String.class).select("orderId").distinct().list();
		if (values.size() > 0) {
			return values;
		}
		return null;
	}

}
